#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int deleteAndEarn(vector<int>& nums)
    {
        int n = nums.size();
        if (n == 0)
        {
            return 0;
        }
        int N = 10001;
        vector<int> arr(N);
        for (int i = 0; i < n; i++)
        {
            arr[nums[i]] += nums[i];
        }
        vector<int> f(N);
        vector<int> g(N);
        f[0] = arr[0];
        for (int i = 1; i < N; i++)
        {
            f[i] = g[i - 1] + arr[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[N - 1], g[N - 1]);
    }
};